# 国家医保服务平台-定点零售药店查询 ：https://fuwu.nhsa.gov.cn/nationalHallSt/#/search/pharmacies?code=90000&flag=false&gbFlag=true


import requests
import execjs

cookies = {
    '__jsluid_s': '917702252f0d45fa51e420e81bc996dc',
    'amap_local': '310000',
    'gb_nthl_sessionId': '1e5da325369e4ce1a0acfe7db6cdd1c7',
    'yb_header_active': '-1',
}

headers = {
    'Accept': 'application/json',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    # 'Cookie': '__jsluid_s=917702252f0d45fa51e420e81bc996dc; amap_local=310000; gb_nthl_sessionId=1e5da325369e4ce1a0acfe7db6cdd1c7; yb_header_active=-1',
    'Origin': 'https://fuwu.nhsa.gov.cn',
    'Pragma': 'no-cache',
    'Referer': 'https://fuwu.nhsa.gov.cn/nationalHallSt/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
    'X-Tingyun': 'c=B|4Nl_NnGbjwY;x=f6da96f2e7e74152',
    'channel': 'web',
    'contentType': 'application/x-www-form-urlencoded',
    'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'x-tif-nonce': 'b4dHltiR',
    'x-tif-paasid': 'undefined',
    'x-tif-signature': '056d8f3851a26e8ae080b408e494c1c2abdbebe21e57fd06e445ed371703153a',
    'x-tif-timestamp': '1700530827',
}
js_file = open('逆向JS.js', 'r', encoding='utf-8').read()


def encrypt_data():
    data = {
        "appCode": "T98HPCGN5ZVVQBS8LZQNOAEXVI9GYHKQ",
        "data": {
            "addr": "",
            "medinsLvCode": "",
            "medinsName": "",
            "medinsTypeCode": "",
            "openElec": "",
            "pageNum": 1,
            "pageSize": 10,
            "queryDataSource": "es",
            "regnCode": "310000"
        },
        "encType": "SM4",
        "signType": "SM2",
        "version": "1.0.0"
    }

    data = execjs.compile(js_file).call('encrypt_data', data)
    return data

# 代理有效时间为 30分钟
proxies = {
    "http": "http://218.95.39.41:11406",
}

response = requests.post(
    'https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/CommQuery/queryFixedHospital',
    cookies=cookies,
    headers=headers,
    proxies=proxies,
    json=encrypt_data()
)

response_data = response.json()

decrypt_data = execjs.compile(js_file).call('decrypt_data', response_data)

for item in decrypt_data['list']:
    print(item)